package binarycrew.livewallpapers.wallpapers;

import java.util.ArrayList;

public class Wallpapers {
    private static ArrayList<Class> wallpapers = new ArrayList<Class>();
    private static ArrayList<WallpaperInfo> wallpaperInfos = new ArrayList<WallpaperInfo>();
    private static Boolean firstRun = true;

    public Wallpapers(){
        initialise();
    }

    public static void initialise(){
        if(!firstRun) return;

        wallpapers.add(LiveWallpaper_1.class);
        wallpapers.add(LiveWallpaper_2.class);
        wallpapers.add(PhotoLiveWallpaper.class);

        // get wallpaper names
        for(Class clazz : wallpapers){
            try {
                ALiveWallpaper wallpaper = (ALiveWallpaper) clazz.newInstance();
                
                WallpaperInfo info = new WallpaperInfo();
                info.setName(wallpaper.getName());
                info.setDescription(wallpaper.getDescription());
                info.setCode(wallpaper.code);
                info.setClazz(clazz);

                wallpaperInfos.add(info);

                wallpaper = null;
            } catch (Exception e) {
            }
        }

        firstRun = false;
    }

    public ArrayList<WallpaperInfo> getList(){
        initialise();

        return wallpaperInfos;
    }

    public static ALiveWallpaper findWallpaperByCode(String code){
        initialise();

        for(WallpaperInfo i : wallpaperInfos) {
            if(i.getCode().equals(code)) {
                try {
                    return (ALiveWallpaper) i.getClazz().newInstance();
                } catch (Exception e) {
                }
            }
        }
        return null;
    }
}
